<?php
/**
 * @author dinglin
 * 申请招聘处理延期
 * 规则：流程没有结束，最近一次处理到现在超过5天，流程撤销。
 */
apf_require_class('Oa_Job_Abstract');
apf_require_class('Const_Flow');
class Oa_Job_Flow_ApplyUserDelay extends Oa_Job_Abstract {

    private $read_db = "hroa_master";//读数据库
    private $writedb = "hroa_master";//写数据库
    public function run(){
        $this->log("招聘申请延期处理--开始 ".date('Y-m-d H:i:s'));
        //取出所有正在进行的招聘流程
        $recruits = $this->_get_recruit_now_flow();
        //数据处理
        $this->_do_delay($recruits);
        $this->log("招聘申请延期处理--结束 ".date('Y-m-d H:i:s'));
    }
    /**
     * 取出所有正在进行的延迟招聘
     */
    private function _get_recruit_now_flow(){
        $pre_5_day = date('Ymd',mktime(0,0,1,date('m'),date('d')-5,date('Y')));
        $sql = "SELECT flow_id FROM apply_recruit WHERE apply_last_opt <".$pre_5_day." AND apply_status=".Const_Flow::FLOW_STATUS_DOING;
        return $this->get_db_rows($sql,$this->read_db,array());
    }
    /**
     * 处理延期
     */
    private function _do_delay($recruits){
        if(empty($recruits)){
            return 0;
        }
        foreach($recruits as $recruit){
            $this->_update_recruit_flow_cancel($recruit['flow_id']);
        }
    }
    /**
     * 执行取消操作
     */
    private function _update_recruit_flow_cancel($flow_id){
        $sql = "UPDATE flow SET now_status=".Const_Flow::FLOW_STATUS_CANCEL_SYSTEM." WHERE flow_id=".$flow_id;
        $this->execute( $sql,array(), $this->writedb);
        $sql = "UPDATE apply_recruit SET apply_status=".Const_Flow::FLOW_STATUS_CANCEL_SYSTEM." WHERE flow_id=".$flow_id;
        $this->execute( $sql,array(), $this->writedb);
    }
}